//
//  WYUserAccountViewModel.swift
//  WYWeibo
//
//  Created by wangyi on 16/7/12.
//  Copyright © 2016年 wangyi. All rights reserved.
//

import UIKit

class WYUserAccountViewModel: NSObject {
    
    //使用单例
    static let sharedUserAccount: WYUserAccountViewModel = WYUserAccountViewModel()
    
    //持有用户对象
    var userAccount: WYUserAccount? {
        //解档 用户对象 获取对象 从沙河看路径获取
        return WYUserAccount.loadUserAccount()
        
    }
    
    //创建私有构造函数
    private override init() {
        super.init()
    }
    
    //
    var isLogin: Bool {
    
        return accessToken != nil
    }
    
    var accessToken: String? {
    
        //
        guard let token = userAccount?.access_token else {
        
            return nil
        }
        
        let result = userAccount?.expiresDate?.compare(NSDate())
        
        if result == NSComparisonResult.OrderedDescending {
        
            return token
        } else {
        
            return nil
        }
    }
    
    //MARK -- 通过授权码(code)获取 accesstoken
    
     func requestAccesstoken(code: String, callBack: (isSucces: Bool) -> ()) {
        
        
        WYNetWorkingTools.sharedTools.requestAccesstoken(code) { (response, error) -> () in
            if error != nil {
                
                print(error)
                return
            }
            //代码执行到此成功的回调
            guard let dic = response as? [String: AnyObject] else{
                
                print("不是一个正确的 json")
                return
                
            }
            
            let userAccount = WYUserAccount(dict: dic)
            
            print(userAccount)
            //通过 accessTpken 和 uid 获取用户信息
            self.requestUserInfos(userAccount,callBack: callBack)
            
        }
    }
    
     func requestUserInfos(userAccount: WYUserAccount, callBack: (isSuccess: Bool) -> ()) {
        
        //根据模型 加载数据  如果得到返回值就执行下面代码
        WYNetWorkingTools.sharedTools.requestUserInfos(userAccount) { (response, error) -> () in
            if error != nil {
                
                print(error)
                
                callBack(isSuccess: false)
                return
            }
            guard let dic = response as? [String: AnyObject] else {
                
                print("这不是一个正确的 JSON ")
                callBack(isSuccess: false)
                return
            }
            
            let name = dic["name"]
            let avatar_large = dic["avatar_large"]
            //给图像赋值
            userAccount.name = name as? String
            userAccount.avatar_large = avatar_large as? String
            
            //保存用户对象
            
          let result = userAccount.saveUserAccount()
            if result {
            
                callBack(isSuccess: true)
            } else {
                //登陆失败
                callBack(isSuccess: false)
                
                printLog("用户保存失败")
            }
            print(userAccount)
            
        }
    }

}
